﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ExamScoreInput.aspx.cs" Inherits="Credit_Teacher_ScoreInput_luofang_ExamScoreInput" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>期末成绩录入</title>
    <link href="../../../Common/css/main.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="../../../Common/js/jquery.js"></script>
    <script type="text/javascript" src="../../../Common/js/common.js"></script>
    <script type="text/javascript" src="../../../Common/js/msg.js"></script>  
    <script type="text/javascript">
    var subjIdList = new Array();
    var subjNameList = new Array();
    var arrayStuIds = new Array();
    var stuNumbers = new Array();
    var stuNames = new Array();
    var exams = new Array();
    var clsName="";
    var examid;
    var eexam;
    var classType;
    var classId;
    var Grade;
    //罗芳。纳入学分的期末成绩上限为100
    var luofangScoreMax = 100;
    function ShowSet()
    {
        ShowPopForm("upInputPop", "upInputPopTitle","导入成绩")   ; 
    }
    function ShowExamInput()
    {
        if(exams.length>0)
        {
            ShowPopForm("upInputPop1", "upInputPopTitle1","导入成绩")   ; 
        }
        else
        {
            showMassgeBox("提示","目前没有可导入成绩的考试",1,null);
        }
    }
    function CheckCh(avalue)
    {
        for   (n=0;   n<avalue.length;   n++) 
        {  
            if   (avalue[n]>128)//   中文字符
            {
                return false;
            }
        }
        return true;
    }
    //焦点移开后判断更新
    function optionSettingScore(obj)
    {
        s=true;
        var id = obj.parentElement.parentElement.id;
        var value = obj.value;

        if( value.search(/^(-|\+)?\d+(\.\d+)?$/)==-1)
        {   

            if(value.length >0 && value.charAt(value.length-1)==".")
            {
                value = value.substr(0,value.length-1);
                if( value.search(/^(-|\+)?\d+(\.\d+)?$/)==-1)
                {
 
                    obj.setAttribute("value",obj.getAttribute("oldvalue")); 
                }
                else
                {
                    if(CheckCh(value))
                    {
                        obj.setAttribute("value",value); 
                        obj.setAttribute("oldvalue",value); 
                    }
                    else
                    {
                        obj.setAttribute("oldvalue",value); 
                    }
                    
                }
            }
            else
            {
                if(value.length == 0 ||  ( /\D/.test(value)))
                {   
                    obj.setAttribute("value",obj.getAttribute("oldvalue")); 
                }
                else
                {
                    obj.setAttribute("value",value); 
                    obj.setAttribute("oldvalue",value); 
                }
            }
           
        }
        else
        {
            obj.setAttribute("oldvalue",value); 
        }
        unModi(obj,'right');
        
        obj.parentElement.parentElement.setAttribute(obj.name, obj.value.length == 0 ? "null":obj.value);
    }

        //非编辑状态
    function unModi(obj,align)
    {
        if(obj.readOnly == false)
        {
            obj.parentElement.style.textAlign = align;
           obj.style.textAlign = align;  
           obj.style.border = 'solid #ffffff 1px;';   
           obj.readOnly = true; 
           updateActivate = 0;   
        }
    }
    //编辑状态
    function modi(obj)
    { 
        if( obj.readOnly == true)
        {
         //   obj.parentElement.style.textAlign = 'center';
            obj.style.textAlign = 'left';  
            obj.style.border = 'solid #a5a5a5 1px;';   
            obj.readOnly = false;   
            obj.select();
            updateActivate = 1; 
        }
    } 
    //保存分数
    function SaveScores()
    {
        var settingTable = document.getElementById('settingTable');
        var arrayStuIds = new Array();
        var scoreArray = new Array();
        for(i=1;i<settingTable.rows.length;i++)
        {
            arrayStuIds[i-1] = settingTable.rows[i].id;
            for(j=0;j<subjIdList.length;j++)
            {
                var temScore = settingTable.rows[i].getAttribute("subject"+subjIdList[j]);
                scoreArray[(i-1)*subjIdList.length+j] = (temScore=="null")? -1:temScore;
            }
        }
        PageMethods.SaveStudentScores(arrayStuIds,subjIdList,scoreArray,classId,function(result){
            showMassgeBox("提示","保存完成",1,null);
        },function(err){showMassgeBox("提示",err._message,1,null);});
    }
    
    function GetExcel()
    {
        var settingTable = document.getElementById('settingTable');

        var scoreArray = new Array();

        for(i=1;i<settingTable.rows.length;i++)
        {
            arrayStuIds[i-1] = settingTable.rows[i].id;
            for(j=0;j<subjIdList.length;j++)
            {
                var temScore = settingTable.rows[i].getAttribute("subject"+subjIdList[j]);
                scoreArray[(i-1)*subjIdList.length+j] = (temScore=="null")? -1:temScore;
            }
            stuNumbers[i-1] = settingTable.rows[i].cells[0].innerText;
            stuNames[i-1] = settingTable.rows[i].cells[1].innerText;
        }
        
        PageMethods.GetExcel(stuNumbers,stuNames,subjIdList,subjNameList,scoreArray,classId,function(result){
            if(result!="")
            {

                document.getElementById("downloadFrame").src = "xml_download.aspx?path="+result;
            //    window.open("xls_files/"+result);
            }
            else
                showMassgeBox("提示","服务器正忙请重试！",2,null);
        },function(err){showMassgeBox("提示",err._message,1,null);});
    }
    //frame string[] 依次为 sheelname，各个 导入列名
    //xls 内各科成绩序列
    //学号  XX  XX  各科成绩
    function LoasExcel(frame,xls)
    {
       
        if(CheckV(frame))
        {
            var va = true;
            for(index=0;index<xls.length;)
            {
                var tarr = new Array();
                for(j=0;j<subjIdList.length;j++)
                {
                    
                    if(xls[index+1+j]>100)
                    {

                        tarr[j]='';
                        va = false;
                    }
                    else
                    {
                        tarr[j]=xls[index+1+j];
                    }
                }
                UpdateOneRow(xls[index],tarr);
                index=index+1+subjIdList.length;
            }
            MyClosePopForm(va)
//             ClosePopForm('upInputPop');

        }
        else
        {
            showMassgeBox("提示","导入格式不正确，请按照模版进行导入！",2,null);
            
        }
        document.getElementById('uploadf').src = "";
        document.getElementById('uploadf').src = "upload.aspx";
    }
    function MyClosePopForm(value)
    {
        ClosePopForm('upInputPop');
        if(!value)
        {
            showMassgeBox("提示","有成绩大于100无法导入！",1,null);
        }
        
    }
    function UpdateOneRow(stuNum,scores)
    {
        var settingTable = document.getElementById('settingTable');
        for(i=1;i<settingTable.rows.length;i++)
        {
            if(settingTable.rows[i].StudentCode == stuNum)
            {
                for(j=0;j<subjIdList.length;j++)
                {
                    settingTable.rows[i].setAttribute("subject"+subjIdList[j],scores[j]==""?"null":scores[j])
                    var tinput = document.getElementById("subject"+subjIdList[j]+"_"+stuNum);
                    if(tinput!=null)
                    {
                        tinput.value = scores[j];
                        tinput.oldvalue = scores[j];
                    }
                }
                break;
            }
            
        }
    }
    //检测输入的excel是不是符合要求
    function CheckV(frame)
    {
        if(frame.length<3 || frame.length != 3+subjNameList.length)
        {
            return false;
        }
        if( frame[1] != "学号" || frame[2] != "姓名")//clsName+"成绩导入" != frame[0] ||
        {
            return false;
        } 
        for(i=0;i<subjNameList.length;i++)
        {
            if(subjNameList[i] != frame[3+i])
            {
                return false;
            } 
        }
        return true;
    }
    
    function ShowError(msg)
    {
        showMassgeBox("提示",msg,2,null);
    }
    function OnKeyPressSecond(keyCode,obj)
    {
        var r = OnTextKeyPress(keyCode,obj);
        if(r)
        {
            var oldStr = obj.value+String(keyCode-48);

            if(parseFloat(oldStr)>luofangScoreMax)
            {
                return false;
            }
        }
        return r;
    }
    //只能输入一位小数
    function OnTextKeyPress(keyCode,obj)
    {
//        if(keyCode==13)         
//          {   
////             document.getElementById(obj.nextid).focus(); 
//             modi(obj);
////             alert(document.getElementById(tbName).value);
//          }   
        var oldStr = obj.value;
        if((keyCode>=48 && keyCode<=57) || keyCode == 46)
        {

            var   sel   =   document.selection;   
            var sekectvar ="";
            if   (sel!=null)   {   
                var   oRng   =   sel.createRange();   
                if   (oRng   !=   null)   
                    sekectvar=oRng.text; 
            }   
                   
            if(oldStr.length>0)
            {

                if(keyCode==46)
                {
                    if(oldStr.indexOf('.')==-1)
                    {
                        
                        return true;
                    }
                    else
                    {   
                    
                        if(sekectvar.indexOf('.')!=-1)
                        {
                            return true;
                        }
                    }
                }
                else 
                {
                                   
                    if(oldStr.indexOf('.')==-1  )
                    {
                     
                        //没有点
                        if(oldStr!=0)
                        {
                        
                            if(oldStr.length<4)
                                return true;
                            else
                            {
                                if(sekectvar.length>0)
                                {
                                    return true;
                                }
                            }
                        }
                        else
                        {
                            if(sekectvar.length>0)
                                {
                                    return true;
                                }
                        }
                    }
                    else
                    {

                        if(sekectvar.indexOf('.')!=-1)
                        {
                            return true;
                        }
//                        if(sekectvar.indexOf('0')!=-1)
//                        {
//                            return true;
//                        }
                        if(oldStr.charAt(oldStr.length-1)==".")
                        {
                            return true;
                        }
                    }
                }
            }
            else
            {
                return keyCode>=48&&keyCode<=57;
            }
        }
        
        return false;
    }
    function movefocus(obj,evt,tbName)           
  {    
//  alert('');

      evt=evt?evt:window.event;    
      if(evt.keyCode==13)         
      {   
         if(document.getElementById(tbName) != null )
         {
            modi(document.getElementById(tbName));
         }
         else
         {
            document.getElementById("settingTable").focus();
         }
      }               
  }
      function InitSelect()
    {
        var sel = document.getElementById('examInfo');
        if(sel!=null)
        {
            for(i=0;i<exams.length;i++)
            {
                var oOption = document.createElement("OPTION");
                oOption.text = exams[i].examName;
                oOption.value = exams[i].examID;
                oOption.examType = exams[i].examType;
                oOption.selected = true;

                sel.add(oOption);
            }
        }
    }
    function OnSelChange()
    {
        var id = event.srcElement.value;
        var examType = event.srcElement.options[event.srcElement.selectedIndex].examType;
        
    }
    function onOkclick()
    {
        var sel = document.getElementById('examInfo');
        if(sel!=null)
        {
            var examType = sel.options[sel.selectedIndex].examType;
            var id = sel.options[sel.selectedIndex].value;
            PageMethods.GetExamScores(examType,id,subjIdList,classId,function(result){
                var r =  UpdateOneRowByexam(result);

                ClosePopForm('upInputPop1');
                if(r)
                {
                    showMassgeBox("提示","有成绩大于100，无法导入！",1,null);
                }
            },function(err){showMassgeBox("提示",err._message,2,null);});
        }
    }
    function GetStuScore(sall,sid)
    {
        for(iii=0;iii<sall.length;iii++)
        {
            if(sall[iii].StudentID == sid)
            {
                return sall[iii];
            }
        }
        return null;
    }
    function UpdateOneRowByexam(stuscoresAll)
    {
       var showMsg = false;
        var settingTable = document.getElementById('settingTable');
        for(i=1;i<settingTable.rows.length;i++)
        {
            var stuscores = GetStuScore (stuscoresAll,settingTable.rows[i].id);
            if(stuscores!=null)
            {
                if(stuscores.Score>luofangScoreMax)
                {
                    showMsg=true;   
                    stuscores.Score = null;
                }
                for(j=0;j<subjIdList.length;j++)
                {
                    settingTable.rows[i].setAttribute("subject"+subjIdList[j],stuscores.Score==null?"null":stuscores.Score)
                    var tinput = document.getElementById("subject"+subjIdList[j]+"_"+ settingTable.rows[i].StudentCode);
                    if(tinput!=null)
                    {
                        tinput.value = stuscores.Score==null?"":stuscores.Score;
                        tinput.oldvalue = stuscores.Score==null?"":stuscores.Score;
                    }
                }

            }
            else
            {
                for(j=0;j<subjIdList.length;j++)
                {
                    settingTable.rows[i].setAttribute("subject"+subjIdList[j],"null")
                    var tinput = document.getElementById("subject"+subjIdList[j]+"_"+ settingTable.rows[i].StudentCode);
                    if(tinput!=null)
                    {
                        tinput.value ="";
                        tinput.oldvalue = "";
                    }
                }

            }
        }
        return showMsg;
    }
    function ResetScore()
    {
        document.getElementById("btnBind").click();
    }
</script>
</head>
<body scroll=auto>
    <form id="form1" runat="server">

        <div class="mainWarp" style="padding-top:2px;height: 88%; overflow: auto; padding-top:5px;">
            <div class="pageBody">
                <div>
                    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
                    </asp:ScriptManager>
                </div>
                <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                    <ContentTemplate>
                        <div>
                             <h4><%= title %> </h4>
                             <input type="button" class="commonBut" id="btnSaveScore" value="保存" onclick="SaveScores()" />
                             <input type="button" class="commonBut" id="Button2" value="重置" onclick="ResetScore();" />
                             <input type="button" class="commonBut"  id="Button1" value="已录入的考试导入" onclick="ShowExamInput()" style="width: 131px" />
                             <input type="button" class="commonBut" id="btnInputScore" value="导入成绩" onclick="ShowSet()" style="width: 76px" />
                             <a onclick="GetExcel()"><u>获得本班的Excel导入模版</u></a>
                             
                        </div>
                        <asp:Repeater ID="Repeater1" runat="server">
                            <HeaderTemplate>
                                <table id="settingTable" border="0" cellpadding="0" cellspacing="1"  style="width:320px;" class="useList">
                                    <tr  >
                                        <%= GetHeader()%>
                                    </tr>
                            </HeaderTemplate>
                            <ItemTemplate>
                                
                                    <%# GetStudent((int)Eval("RID"), Eval("StudentCode").ToString(), Eval("StudentName").ToString())%>
                                
                            </ItemTemplate>
                            <FooterTemplate>
                                </table>
                            </FooterTemplate>
                        </asp:Repeater>
                    </ContentTemplate>
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="btnBind" EventName="Click" />
                    </Triggers>
                </asp:UpdatePanel>
                <div style="display: none;">
                <input id="Text2" type="text" />
                  <asp:Button ID="btnBind" runat="server" OnClick="btnBind_Click" Text="btnBind" />
                  <iframe src="" name="ifram" id="downloadFrame" style="height: 80%;width: 95%;overflow-y:auto;overflow-x:none;" frameborder="0"   ></iframe>
                </div>
    <div id="upInputPop1" class="layoutWarp" style="display: none; width:380px;height:180px;">
         <div class="title" >
	        <span id="upInputPopTitle1"></span>
            <a title="关闭" onclick="return ClosePopForm('upInputPop1')">关闭</a>
          </div>
            <table border="0" cellpadding="0" cellspacing="1"  style="width:300px;" class="useList" >
                <tr>
                <th colspan=2 width="300px" align="left" style="padding-left:5px;text-align:left;">从本学期已录入的考试成绩导入</th>
                </tr>
                <tr>
                    <td width="110px" style="padding-left:5px;text-align:left;">考试名称：</td>
                    <td width="190px" style="padding-left:5px;text-align:left;"><select style="padding-top:5px;padding-left:5px;" width="1750px" id="examInfo" ></select></td>
                </tr>
                <tr>
                    <td colspan=2 width="300px" style="padding-left:5px;text-align:left;"><span style="color:Red">* 得分超过100的成绩将无法导入</span></td>
                </tr>
            </table>
            <div align="center" style="padding-top:2px;">
                <input name="btnOK" type="button" class="commonBut"  id="btnOK" onclick="onOkclick();" value="确定"   />
                <input name="btnCancel" type="button" class="commonBut" id="btnCancel"  onclick="return ClosePopForm('upInputPop1')" value="取消" />
            </div>
            </div>
            </div>
        </div>

    <div id="upInputPop" class="layoutWarp" style="display: none; width:380px;height:230px;">
         <div class="title" >
	        <span id="upInputPopTitle"></span>
            <a title="关闭" onclick="return ClosePopForm('upInputPop')">关闭</a>
               
          </div>
        <iframe src="upload.aspx" name="ifram" id="uploadf" style="height: 80%;width: 95%;overflow-y:auto;overflow-x:none;" frameborder="0"   ></iframe>
    </div>
    

   </form>
</body>
</html>
<script language="javascript">
    <%
        int i = 0;

        Response.Write("subjIdList[0] ="+cause.RID+";");
        Response.Write("subjNameList[0] ='"+cause.CauseName+"';");

        Response.Write("classId=" +  cls.RID +";");

        Response.Write("clsName='"+cls.ClsName+"';");

        for(i=0;i<studentInfo.Count;i++)
        {
            Response.Write("arrayStuIds["+i+"]=" +  studentInfo[i].RID +";");
            Response.Write("stuNumbers["+i+"]='" +  studentInfo[i].StudentCode +"';");
            Response.Write("stuNames["+i+"]='" +  studentInfo[i].StudentName +"';");
        }
        if(examschool!=null)
        {
            int index =0;
            foreach ( Meqs.Entity.ExamsSchool se in examschool)
            {
                string t = this.Server.HtmlDecode(se.Name);
                t = t.Replace("\\", "\\\\");
                t = t.Replace("'", "\\'");
                t = t.Replace("<", "\\<");
                t = t.Replace(">", "\\>");
                Response.Write("exams[" + index + "]={examType:0,examID:"+se.RID+",examName:'" + t + "'};");
                index++;
            }
        }  
     %>
     InitSelect();
</script>